package com.metaapp.demo.view.activity

import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.metaapp.demo.R
import com.metaapp.demo.bean.SearchDataBean
import com.modong.jianjian.base.BaseMvpActivity
import com.modong.jianjian.view.activity.contacts.MainContract
import com.modong.jianjian.view.activity.contacts.MainPresenter
import kotlinx.android.synthetic.main.activity_main.*

/**
 * 主页面
 */
class MainActivity : BaseMvpActivity<MainContract.Presenter>(), MainContract.View {

    private lateinit var mAdapter: SearchPicAdapter
    private var searchContent = "北京"
    override fun createPresenter(): MainContract.Presenter {
        return MainPresenter()
    }

    override val layoutId: Int
        get() = R.layout.activity_main


    override fun initView() {
        super.initView()
        //recyclerview设置布局管理器
        recyclerView.layoutManager = GridLayoutManager(this, 3)
        mAdapter = SearchPicAdapter(this, R.layout.item_search_pic)
        recyclerView.adapter = mAdapter
        //禁止下拉刷新
        srlayout.setEnableRefresh(false)

    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        //请求数据
        mPresenter?.getSearchPic(searchContent)
    }

    /**
     * 设置监听事件
     */
    override fun setListener() {
        super.setListener()
        //搜索
        btn_search.setOnClickListener {
            searchContent = et_input_search.text.toString().trim()
            //判断搜索内容是否为空
            if (TextUtils.isEmpty(searchContent)) {
                Toast.makeText(this, getString(R.string.search_not_content), Toast.LENGTH_SHORT)
                    .show()
                return@setOnClickListener
            }
            mPresenter?.getSearchPic(searchContent)
        }
        //加载更多
        srlayout.setOnLoadMoreListener {
            mPresenter?.loadMoreData(searchContent)

        }
    }

    /**
     * 获取数据成功回调
     */
    override fun onSearchPicSuccess(data: SearchDataBean) {
        mAdapter.setData(data.list)
    }

    /**
     * 加载更多返回成功数据
     */
    override fun onLoadmoreSuccess(data: SearchDataBean) {
        srlayout.finishLoadMore()
        mAdapter.setLoadMoreDate(data.list)
    }

}